<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="app">
    <template>
        <el-header height="15px">车站管理
        </el-header>
        <el-divider></el-divider>
        <el-form :inline="true" :model="searchForm" class="demo-form-inline" >
            <el-form-item label="车站中文名" prop="nameInChinese">
                <el-input v-model="searchForm.nameInChinese" ></el-input>
            </el-form-item>
            <el-form-item label="车站英文名" prop="nameInEnglish">
                <el-input v-model="searchForm.nameInEnglish" ></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="selectAgain">查询</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="danger" plain @click="deleteByIds" style="margin-left: 60%">批量删除</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" plain @click="dialogFormVisible = true" style="margin-left: 80%">新增</el-button>
            </el-form-item>
        </el-form>

        <el-table
                :data="tableData"
                stripe
                style="width: 100%"
                ref="multipleTable"
                tooltip-effect="dark"
                @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    prop="nameInChinese"
                    label="车站名称"
                    min-width="180">
            </el-table-column>
            <el-table-column
                    prop="nameInEnglish"
                    label="车站英文名称"
                    min-width="250">
            </el-table-column>
            <el-table-column
                    align="center"
                    label="操作"
                    >
                <template slot-scope="scope">
                    <el-row>
                        <el-button type="primary" @click="dialogFormVisible1 = true;handleEdit(scope.$index, scope.row)">修改</el-button>
                        <el-button type="danger" @click="deleteById(scope.$index, scope.row)">删除</el-button>
                    </el-row>
                </template>
            </el-table-column>
        </el-table>
    </template>
    <div class="block">
        <el-pagination
                @current-change="handleCurrentChange"
                :current-page="currentPage1"
                layout="prev, pager, next"
                :page-size="pageSize"
                :total="totalCount"
        >
        </el-pagination>
    </div>

    <el-dialog title="创建车站"
               :visible.sync="dialogFormVisible" width="40%"
               :close-on-click-modal="false"
               :before-close="cleanModel">
        <el-form :model="createForm" :rules="rules">
            <el-form-item label="车站中文名" :label-width="'100px'" prop="nameInChinese">
                <el-input v-model="createForm.nameInChinese" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="车站英文名" :label-width="'100px'" prop="nameInEnglish">
                <el-input v-model="createForm.nameInEnglish" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="cleanModel">取 消</el-button>
            <el-button type="primary" @click="create">确 定</el-button>
        </div>
    </el-dialog>

    <el-dialog title="修改车站信息"
               :visible.sync="dialogFormVisible1" width="40%"
               :close-on-click-modal="false"
               :before-close="cleanModel">
        <el-form :model="createForm" :rules="rules">
            <el-form-item label="车站中文名" :label-width="'100px'" prop="nameInChinese">
                <el-input v-model="createForm.nameInChinese" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="车站英文名" :label-width="'100px'" prop="nameInEnglish">
                <el-input v-model="createForm.nameInEnglish" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="cleanModel">取 消</el-button>
            <el-button type="primary" @click="update">确 定</el-button>
        </div>
    </el-dialog>

</div>
</body>

<script src="../../../js/axios-0.18.0.js"></script>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue@2/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="../../js/request.js"></script>
<script>

    new Vue({
        el: '#app',
        mounted(){
            this.select();
        },
        data() {
            return {
                //activeIndex: '1',
                //activeIndex2: '1',
                dialogFormVisible: false,
                dialogFormVisible1: false,
                pageSize : 15,
                currentPage1 : 1,
                totalCount : 40,
                rules: {
                    nameInChinese: [
                        { required: true, message: '请输入车站名', trigger: 'blur' }
                    ],
                    nameInEnglish: [
                        { required: true, message: '请输入车站英文名称', trigger: 'blur' }
                    ]
                },
                searchForm : [{
                    nameInChinese: '',
                    nameInEnglish: ''
                }],
                tableData: [{
                    id: '1',
                    nameInChinese: '临港东站',
                    nameInEnglish: 'LingGang East Station'
                }, {
                    id: '2',
                    nameInChinese: '临港西站',
                    nameInEnglish: 'LingGang West Station'
                }, {
                    id: '3',
                    nameInChinese: '临港南站',
                    nameInEnglish: 'LingGang South Station'
                }, {
                    id: '4',
                    nameInChinese: '临港北站',
                    nameInEnglish: 'LingGang North Station'
                }],
                createForm:{
                    nameInChinese: '',
                    nameInEnglish: ''
                },
                multipleSelection: [],
                selectedIds: []
            }
        },
        methods: {

            cleanModel(){
                this.createForm={
                    nameInChinese: '',
                    nameInEnglish: ''
                };
                this.selectedIds=[];
                this.dialogFormVisible=false;
                this.dialogFormVisible1=false;
            },

            selectAgain(){
                this.currentPage1=1;
                this.select();
            },

            handleCurrentChange(val) {
                this.currentPage1=val;
                this.select();
            },

            handleSelect(key, keyPath) {
                console.log(key, keyPath);
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            handleEdit(index,row){
                this.createForm={
                    id: this.tableData[index].id,
                    nameInChinese: this.tableData[index].nameInChinese,
                    nameInEnglish: this.tableData[index].nameInEnglish
                };
            },
            select(){
                var _this=this;
                axios({
                    method:"get",
                    url:"http://localhost:8080/station/page",
                    params: {
                        page: this.currentPage1,
                        nameInChinese: this.searchForm.nameInChinese,
                        nameInEnglish: this.searchForm.nameInEnglish
                    },

                }).then(function (resp) {
                    _this.tableData=resp.data.data.records;
                    _this.totalCount=resp.data.data.total;
                    _this.pageSize=resp.data.data.size;
                })
            },
            create(){
                var _this=this;
                axios({
                    method:"put",
                    url:"http://localhost:8080/station/create",
                    data: _this.createForm
                }).then(function (resp) {
                    if(resp.data.code === 1){
                        _this.$message({
                            showClose: true,
                            message: '创建成功！',
                            type: 'success'
                        });
                        _this.dialogFormVisible=false;
                        _this.cleanModel();
                        _this.selectAgain();
                    }else {
                        _this.$message({
                            showClose: true,
                            message: '车站已存在！',
                            type: 'error'
                        });
                    }
                });

            },
            deleteById(index,row){
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    var id = this.tableData[index].id;
                    var _this = this;
                    axios({
                        method: "delete",
                        url: "http://localhost:8080/station",
                        params: { id }
                    }).then(function (resp) {
                        if (resp.data.code === 1) {
                            _this.select();
                            _this.$message({
                                message: '删除成功！',
                                type: 'success'
                            });

                        }

                    })
                    this.cleanModel();
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            deleteByIds(){
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    for(let i=0;i<this.multipleSelection.length;i++){
                        let selectionElement =this.multipleSelection[i];
                        this.selectedIds[i]=selectionElement.id;
                    }
                    var _this=this;
                    axios({
                        method:"post",
                        url:"http://localhost:8080/station/deleteByIds",
                        data:_this.selectedIds
                    }).then(function (resp) {
                        if(resp.data.code===1){
                            _this.select();
                            _this.$message({
                                message: '删除成功！',
                                type: 'success'
                            });
                        }

                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            update(){
                var _this=this;
                axios({
                    method:"put",
                    url:"http://localhost:8080/station/update",
                    data: _this.createForm
                }).then(function (resp) {
                    if(resp.data.code === 1){
                        _this.$message({
                            showClose: true,
                            message: '修改成功！',
                            type: 'success'
                        });
                        _this.dialogFormVisible1=false;
                        _this.cleanModel();
                        _this.select();

                    }else {
                        _this.$message({
                            showClose: true,
                            message: '修改失败',
                            type: 'error'
                        });
                    }
                });

            }
        }
    })
</script>



</html>